//
//  SYDResetTheWordController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/12.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDResetTheWordController.h"

@interface SYDResetTheWordController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldPassWord;
@property (weak, nonatomic) IBOutlet UITextField *resetWord;
@property (weak, nonatomic) IBOutlet UITextField *resetWordTwo;

@property (weak, nonatomic) IBOutlet UIView *oldView;
@property (weak, nonatomic) IBOutlet UIView *resetView;
@property (weak, nonatomic) IBOutlet UIView *resetViewTwo;

@property (weak, nonatomic) IBOutlet UIButton *resetBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnConstraint;

@end

@implementation SYDResetTheWordController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationBar.title = @"重置登录密码";
    [self navigationBarHandle];
    [self initResetWordUI];
}
-(void)initResetWordUI{
    if (iPhone8_X) {
        self.topConstraint.constant = 88;
        self.btnConstraint.constant = 58;
    } else {
        self.topConstraint.constant = 64;
        self.btnConstraint.constant = 48;
    }
    
    [self.oldView addBottomLineWithLeft:15 right:15];
    [self.resetView addBottomLineWithLeft:15 right:15];
    [self.resetViewTwo addBottomLine];
    
//    self.oldPassWord.placeholderColor = GRAY_COLOR;
//    self.resetWord.placeholderColor = GRAY_COLOR;
//    self.resetWordTwo.placeholderColor = GRAY_COLOR;
    
    self.oldPassWord.textColor = SUB_TITLE_COLOR;
    self.resetWord.textColor = SUB_TITLE_COLOR;
    self.resetWordTwo.textColor = SUB_TITLE_COLOR;
    
    self.oldPassWord.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.resetWord.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.resetWordTwo.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    self.oldPassWord.secureTextEntry = YES;
    self.resetWord.secureTextEntry = YES;
    self.resetWordTwo.secureTextEntry = YES;
    //WEAKSELF
    if (self.oldPassWord.text.length > 0) {
        [self.oldPassWord.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"旧密码:%@",x);
        }];
    }
    
    if (self.resetWord.text.length > 0) {
        [self.resetWord.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"新密码:%@",x);
        }];
    }
    
    if (self.resetWordTwo.text.length > 0) {
        [self.resetWordTwo.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"再次确认密码:%@",x);
        }];
    }
    WEAKSELF
    [[self.resetBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        //判断3个密码是否为空
        //1.判断旧密码是否正确\
        //2.判断密码输入规则是否正确
        //3.判断新密码与第二次输入的密码是否相符
        if (self.oldPassWord.text.length == 0) {
            [SVProgressHUD displayInfoWithStatus:@"请输入旧密码"];
        }else if (![[NSString sha1:self.oldPassWord.text] isEqualToString:self.userAccountModel.password]) {
            [SVProgressHUD displayInfoWithStatus:@"旧密码输入错误"];
        }else if (self.resetWord.text.length < 6){
            [SVProgressHUD displayInfoWithStatus:@"输入密码不符合规则,密码长度需大于6位"];
        }else if (self.resetWordTwo.text.length < 6){
            [SVProgressHUD displayInfoWithStatus:@"确认密码不符合规则,密码长度需大于6位"];
        }else if (![self.resetWord.text isEqualToString:self.resetWordTwo.text]){
            [SVProgressHUD displayInfoWithStatus:@"两次输入的密码不同,请重新输入"];
        }else{
            [[SYDNetwork shareTool]SetMineAccountPwdWithCityCode:[YMSaveTool valueForKey:CityCode] oldPwd:self.oldPassWord.text password:self.resetWordTwo.text userId:[YMSaveTool valueForKey:USER_ID] success:^(NSURLSessionDataTask *task, id responseObject) {
                @try {
                    NSLog(@"--%@",responseObject);
                    if ([responseObject[@"statusCode"]integerValue] == 200) {
                        [SVProgressHUD displayInfoWithStatus:@"重置密码成功"];
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        });
                    }else{
                        [SVProgressHUD displayInfoWithStatus:responseObject[@"msg"]];
                    }
                } @catch (NSException *exception) {
                    NSLog(@"--%@",exception.description);
                } @finally {
                }
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                NSLog(@"error:%@",error);
            }];
        }
        
        
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
